package legstar.trans.choice;

import java.util.Hashtable;

import com.legstar.coxb.CobolElementVisitor;
import com.legstar.coxb.ICobolBinding;
import com.legstar.coxb.ICobolChoiceBinding;
import com.legstar.coxb.ICobolUnmarshalChoiceStrategy;
import com.legstar.coxb.host.HostException;

import legstar.trans.rdef01.Rdef01Record;

/**
 * Skeleton implementation of a custom choice selection strategy. Modify this
 * code to select a suitable alternative.
 */
public class ComDetailChoice implements ICobolUnmarshalChoiceStrategy {

    /** {@inheritDoc} */
    public ICobolBinding choose(final ICobolChoiceBinding choice,
            final Hashtable<String, Object> variablesMap,
            final CobolElementVisitor visitor) throws HostException {

        /*
         * Get the parent value object which properties might help select the
         * right alternative.
         */
        Rdef01Record valueObject = (Rdef01Record) choice.getParentValueObject();
        assert (valueObject != null);

        /* Replace following code with actual logic. */
        int index = valueObject.getComSelect();
        switch (index) {
        case 0:
            return choice.getAlternativeByName("ComDetail1");
        case 1:
            return choice.getAlternativeByName("ComDetail2");
        case -1:
            /* An exemple of how to signal an exception. */
            throw (new HostException("Unable to select an alternative"));
        default:
            /* Null will let the default choice strategy apply. */
            return null;
        }
    }
}
